<template>
	<view class="shopping">
		<topbar-shopping @delSelect="delSelect"></topbar-shopping>
		<view class="shopping-img"><image src="../../static/images/shopbag_bg.png" mode="widthFix"></image></view>
		<content-shopping :cartList="cartList" @refreshCount="refreshCount" @deleteMall="deleteMall" :delBtn="delBtn"></content-shopping>
	</view>
</template>

<script>
	import TopbarShopping from "./childShopping/TopbarShopping.vue"
	import ContentShopping from "./childShopping/ContentShopping.vue"
	
	import {getAllCart,postRefreshCount,postDelShopMall} from "../../network/cart.js"
	
	export default {
		onPullDownRefresh(){
			this._getAllCart()
			uni.stopPullDownRefresh()
		},
		onLoad(){
			this._getAllCart()
		},
		activated() {
			this._getAllCart()
		},
		data() {
			return {
				cartList:[],
				delBtn:true
			}
		},
		components:{
			TopbarShopping,
			ContentShopping,
		},
		methods:{
			refreshCount(e){
				postRefreshCount(e.sid,e.count).then(res=>{
					if(res.data.code !== 6000){
						uni.showToast({
						    title: res.data.msg,
						    duration: 2000,
							icon:'none'
						})
					}
				})
			},
			_getAllCart(){
				getAllCart().then(res=>{
					this.cartList=res.data.result
					this.cartList.forEach((element) => {
							this.$set(element,"checked",true)
						  });
				})
			},
			delSelect(){
				this.delBtn = !this.delBtn
			},
			deleteMall(e){	
				let sids = JSON.stringify(e)
				postDelShopMall(sids).then(res=>{
					if(res.data.code !== 7000){
						uni.showToast({
						    title: res.data.msg,
						    duration: 2000,
							icon:'none'
						})
					}
				})
				let newCartList = this.cartList.filter(e => e.checked == false)
				this.cartList = newCartList
			}
		}
	}
</script>

<style scoped>
	.shopping{
		height: 100vh;
		position: relative;
	}
	.shopping-img>image{
		width: 100%;
	}
	
</style>
